/**
 * 
 */
package com.snsoft.service.duty;

import java.sql.SQLException;
import java.util.List;

import com.snsoft.bean.User;
import com.snsoft.bean.UserInfo;
import com.snsoft.dao.DutyDao;
import com.snsoft.dao.UserDao;
import com.snsoft.exception.PromiseRefused;
import com.snsoft.exception.UserExist;

/**
 * @author 苏国启
 * 
 * @date 2017年11月9日 下午8:20:36
 * 
 * @Description TODO 删除用户的业务逻辑
 * 
 */
public class DeleteUserService {

	/**
	 * 删除指定用户，即将其deleteflag置为0
	 * 
	 * @param userIDstr
	 * @param admin
	 * @throws UserExist
	 * @throws PromiseRefused
	 * @throws SQLException
	 */
	public void deleteUser(String userIDstr, UserInfo admin) throws UserExist, PromiseRefused, SQLException {
		User user = null;
		int role;
		DutyDao dd = new DutyDao();
		int userID = Integer.parseInt(userIDstr);
		if ((user = new UserDao().getUser(userIDstr)) == null) {
			throw new UserExist("目标用户不存在！");
		}
		if ((role = user.getRole()) == 1) {
			throw new PromiseRefused("您无权删除超级管理员！");
		}
		if (admin.getRole() == 2) {
			List<Integer> companyids = dd.getUserCompanyIds(admin.getUserID());
			List<Integer> usercompany = dd.getUserCompanyIds(userID);
			if (role == 2 || companyids.isEmpty() || usercompany.isEmpty()
					|| !companyids.contains(usercompany.get(0))) {
				throw new PromiseRefused("您无权删除该用户！");
			}
		}
		dd.deleteuser(userID);
	}

}
